using System.Collections;
using UnityEngine;
using Unity.VisualScripting;
using GameFramework;
using GameFramework.DataTable;
using UnityGameFramework.Runtime;

namespace LS.UGFUnits
{
    [UnitShortTitle("Data Table Row")]
    [UnitSurtitle("Check Existed")]
    public sealed class CheckDataTableRowExisted : DataTableUnit
    {
        protected override bool UseOutput => false;
        
        [Serialize]
        [Inspectable, UnitHeaderInspectable]
        [TypeFilter(typeof(DataRowBase))]
        public System.Type DataTableType { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Id { get; private set; }
        
        [DoNotSerialize]
        public ControlOutput Existed { get; private set; }
        
        [DoNotSerialize]
        public ControlOutput NotExisted { get; private set; }

        protected override void Definition()
        {
            base.Definition();
            Existed = ControlOutput(nameof(Existed));
            NotExisted = ControlOutput(nameof(Existed));
            Id = ValueInput<int>(nameof(Id), 0);
            Requirement(Id, Input);
        }
        private ControlOutput In(Flow flow)
        {
            int id = flow.GetValue<int>(Id);
            DataTableBase dataTable = Target?.GetDataTable(DataTableType);
            bool result = false;
            if (dataTable != null)
            {
                foreach (IDataRow dataRow in (IEnumerable)dataTable)
                {
                    if (dataRow.Id == id)
                    {
                        result = true;
                        return Existed;
                    }   
                }
            }
            return NotExisted;
        }
    }
}